#include<unistd.h>
#include<iostream>
#include<errno.h>
#include<sys/types.h>
#include<sys/wait.h>
#include<string.h>
#include<string>
using namespace std;

int main()
{
  pid_t id = fork();
  if (id < 0)
  {
    perror("fork");
    return 1;
  }
  else if (id == 0)
  {
    //子进程
    int cnt = 5;
    while (cnt)
    {
      cout<<getpid()<<"<-"<<getppid()<<endl;
      cnt--;
      sleep(1);
    }
    return 1;
    exit(1);
  }
  else 
  {
     //父进程
     int cnt = 10;
     while (cnt)
     {
       cout<<getpid()<<"<-"<<getppid()<<endl;
       cnt--;
       sleep(1);
     }
    int error_r = 0; 
    pid_t cid = wait(&error_r);
    if (error_r)
    {
      cout<<"子进程返回1状态"<<"->"<<error_r<<endl;
    }
    if (cid == id)
    {
      cout<<"success"<<endl;
    }
    else 
    { 
      cout<<strerror(errno)<<endl;
    }
    
  }
  return 0;
}
